import hashlib,time,requests,json,execjs
from  dao import my_sql
import uuid
def md5(value):
    m = hashlib.md5(value.encode(encoding='utf8'))
    return m.hexdigest()
def getSecuritykey(pai):
    print(pai)
    return md5(str(pai)+ "rmj$cd&2e09elp468b7b87dlsye#jipl")

def parse(page):
    # 接口请求时间
    refresh_time = str(int(time.time()))
    # 请求页
    # 获取加密参数值
    a = execjs.compile('''
            function ce(e) {
                var t = e
                  , n = "";
                for (var i in t)
                    n += i + "=" + t[i] + "|";
                n = n.substring(0, n.length - 1)
                return n
            }
    ''')
    da={'category_id': "1",
        'interface_code': 610,
        'page': page,
        'refresh_time': refresh_time,
        'show_num': "10"}
    cookie=a.call('ce',da)
    securitykey = getSecuritykey(pai=cookie)
    # 拼装请求url
    url = "https://app.peopleapp.com/WapApi/610/HomeApi/getContentList?category_id=1&refresh_time=" + str(refresh_time) + "&show_num=10&page=" + str(page) + "&securitykey=" + securitykey + "&interface_code=610"
    res = json.loads(requests.get(url).text)

    for res_info in res['hots']:
        uid = uuid.uuid4()
        hots = '1'
        # articleid=res_info['articleid']
        news_timestamp=res_info['news_timestamp']
        title=res_info['title']
        imgs=res_info['image']
        my_sql.insert_people(uid=uid,hot=hots,time=news_timestamp,title=title)
        for img_ in imgs:
            url=img_['url']
            descs=img_['desc']
            my_sql.insert_people_img(uid=uid,descs=descs,img=url)
    for res_info in res['data']:
        uid = uuid.uuid4()
        hots='0'
        # articleid=res_info['articleid']
        news_timestamp=res_info['news_timestamp']
        title=res_info['title']
        imgs=res_info['image']
        my_sql.insert_people(uid=uid, hot=hots, time=news_timestamp, title=title)
        for img_ in imgs:
            url=img_['url']
            descs=img_['desc']
            my_sql.insert_people_img(uid=uid, descs=descs, img=url)

parse(page=1)

# def save_info():
